'''
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with self work for additional information
 * regarding copyright ownership.  The ASF licenses self file
 * to you under the Apache License, Version 2.0 (the
 * "License") you may not use self file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http:#www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
'''
from base.graph.NodeFactory import NodeFactory
from base.rdf.Selector import Selector
from base.rdf.impl.LiteralImpl import LiteralImpl

#import org.apache.jena.graph.*;
#import org.apache.jena.rdf.model.impl.*;

'''
 * A general selector class for use when querying models.
 *
 * <p>An instance of self class is passed with query calls to models.  The model
 * will use the <CODE>test</CODE> method of self class to decide whether
 * a statement should be included in the selection.
 *
 * <p>Instances of self class can be provided with subject, predicate and object
 * constraints.  If a subject, a predicate or an object are provided,
 * the model implementation <b>may</b> restrict the statements that it tests
 * to statements whose subject, predicate and object match those provided in
 * the constructor.  This can provide for considerably more efficient
 * searching.  However, the model implementation is not required to do self.
 * If no subject, predicate or object are provided in
 * the constructor, then all statements in the model must be tested.
 *
 * <p>This class is designed to be subclassed by the application, defining
 * defining further selection criteria of its own by providing its own
 * <CODE>selects</CODE> method.
 *
 * <p>A direct instance of SimpleSelector returns <code>True</code> for the
 * Selector::isSimple() predicate. Instances of subclasses of SimpleSelector
 * return <code>False</code>, since the only reason to have such subclasses
 * is to provide a non-trivial <code>test</code> predicate or S/P/O tests other
 * than equality.
 *
 * <p>The <CODE>test</CODE> method first verifies that a statement satisfies
 * any subject, predicate or object constraints and the calls the <CODE>
 * selects</CODE> method to test for any application supplied constraint.  The
 * default <CODE>selects</CODE> method simply returns True.
'''


class SimpleSelector(Selector):

    # protected Resource subject
    # protected Property predicate
    # protected RDFNode object

    '''
     * Create a selector.  Since no subject, predicate or object constraints are
     * specified a model will test all statements.
    '''
    # param 
    def __init__(self):
        self.subject = None
        self.predicate = None
        self.obj = None
    

    '''
     * Create a selector.  A model <b>may</b> restrict statements that are tested using
     * the <CODE>selects</CODE> method to those whose subject matches the
     * subject parameter, whose predicate matches the predicate parameter and whose
     * object matches the object paramater.  Any None parameter is considered to
     * match anything.
     *
     * @param subject   if not None, the subject of selected statements
     *                  must equal self argument.
     * @param predicate if not None, the predicate of selected statements
     *                  must equal self argument.
     * @param object    if not None, the object of selected statements
     *                  must equal self argument.
    '''
    # param Resource subject, Property predicate, RDFNode object
    def initWithSPO(self, subject, predicate, obj):
        self.subject = subject
        self.predicate = predicate
        self.obj = obj
    

    '''
     * Create a selector.  A model <b>may</b> restrict statements that are tested using
     * the <CODE>selects</CODE> method to those whose subject matches the
     * subject parameter, whose predicate matches the predicate parameter and whose
     * object matches the object paramater.  Any None parameter is considered to
     * match anything.
     *
     * @param subject   if not None, the subject of selected statements
     *                  must equal self argument.
     * @param predicate if not None, the predicate of selected statements
     *                  must equal self argument.
     * @param object    if not None, the object of selected statements
     *                  must equal self argument.
    '''
    # param Resource subject, Property predicate, boolean object
    # def __init__(self, subject, predicate, obj):
    #     self(subject, predicate, String.valueOf(obj)
    

    '''
     * Create a selector.  A model <b>may</b> restrict statements that are tested using
     * the <CODE>selects</CODE> method to those whose subject matches the
     * subject parameter, whose predicate matches the predicate parameter and whose
     * object matches the object paramater.  Any None parameter is considered to
     * match anything.
     *
     * @param subject   if not None, the subject of selected statements
     *                  must equal self argument.
     * @param predicate if not None, the predicate of selected statements
     *                  must equal self argument.
     * @param object    the object of selected statements
     *                  must equal self argument.
    '''
    # param Resource subject, Property predicate, long object
    # def __init__(self, subject, predicate, obj):
    #     self(subject, predicate, String.valueOf(obj)
    

    '''
     * Create a selector.  A model <b>may</b> restrict statements that are tested using
     * the <CODE>selects</CODE> method to those whose subject matches the
     * subject parameter, whose predicate matches the predicate parameter and whose
     * object matches the object paramater.  Any None parameter is considered to
     * match anything.
     *
     * @param subject   if not None, the subject of selected statements
     *                  must equal self argument.
     * @param predicate if not None, the predicate of selected statements
     *                  must equal self argument.
     * @param object    the object of selected statements
     *                  must equal self argument.
    '''
    # param Resource subject, Property predicate, char object
    # def __init__(self, subject, predicate, obj):
    #     self(subject, predicate, String.valueOf(obj)
    

    '''
     * Create a selector.  A model <b>may</b> restrict statements that are tested using
     * the <CODE>selects</CODE> method to those whose subject matches the
     * subject parameter, whose predicate matches the predicate parameter and whose
     * object matches the object paramater.  Any None parameter is considered to
     * match anything.
     *
     * @param subject   if not None, the subject of selected statements
     *                  must equal self argument.
     * @param predicate if not None, the predicate of selected statements
     *                  must equal self argument.
     * @param object    the object of selected statements
     *                  must equal self argument.
    '''
    # param Resource subject, Property predicate, float object
    # def __init__(self, subject, predicate, obj):
    #     self(subject, predicate, String.valueOf(obj)
    

    '''
     * Create a selector.  A model <b>may</b> restrict statements that are tested using
     * the <CODE>selects</CODE> method to those whose subject matches the
     * subject parameter, whose predicate matches the predicate parameter and whose
     * object matches the object paramater.  Any None parameter is considered to
     * match anything.
     *
     * @param subject   if not None, the subject of selected statements
     *                  must equal self argument.
     * @param predicate if not None, the predicate of selected statements
     *                  must equal self argument.
     * @param object    the object of selected statements
     *                  must equal self argument.
    '''
    # param Resource subject, Property predicate, double object
    # def __init__(self, subject, predicate, obj):
    #     self(subject, predicate, String.valueOf(obj)
    

    '''
     * Create a selector.  A model <b>may</b> restrict statements that are tested using
     * the <CODE>selects</CODE> method to those whose subject matches the
     * subject parameter, whose predicate matches the predicate parameter and whose
     * object matches the object paramater.  Any None parameter is considered to
     * match anything.
     *
     * @param subject   if not None, the subject of selected statements
     *                  must equal self argument.
     * @param predicate if not None, the predicate of selected statements
     *                  must equal self argument.
     * @param object    the object of selected statements
     *                  must equal self argument - a None string matches the empty string
    '''
    # param Resource subject, Property predicate, String object
    # def __init__(self, subject, predicate, obj):
    #     self(subject, predicate, object, "")
    

    '''
     * Create a selector.  A model <b>may</b> restrict statements that are tested using
     * the <CODE>selects</CODE> method to those whose subject matches the
     * subject parameter, whose predicate matches the predicate parameter and whose
     * object matches the object paramater.  Any None parameter is considered to
     * match anything.
     *
     * @param subject   if not None, the subject of selected statements
     *                  must equal self argument.
     * @param predicate if not None, the predicate of selected statements
     *                  must equal self argument.
     * @param object    the object of selected statements
     *                  must equal self argument - the None string matches the empty string
     * @param language  the language of the object constraint
    '''
    # param Resource subject, Property predicate,
    def initWithSPOAndLanguage(self, subject, predicate, obj, language):
        self.subject = subject
        self.predicate = predicate
        if (obj is not None):
            self.obj = self.literal(obj, language)
        else:
            self.obj = None
        
    

    # param String s, String lang
    # result Literal
    def literal(self, s, lang):
        return LiteralImpl(NodeFactory.createLiteralByValue(s, lang, None), None)
    

    '''
     * Create a selector.  A model <b>may</b> restrict statements that are tested using
     * the <CODE>selects</CODE> method to those whose subject matches the
     * subject parameter, whose predicate matches the predicate parameter and whose
     * object matches the object paramater.  Any None parameter is considered to
     * match anything.
     *
     * @param subject   if not None, the subject of selected statements
     *                  must equal self argument.
     * @param predicate if not None, the predicate of selected statements
     *                  must equal self argument.
     * @param object    if not None, the object of selected statements
     *                  must equal self argument.
    '''
    # param Resource subject, Property predicate, Object object
    # def __init__(self, subject, predicate, obj):
    #     self.subject = subject
    #     self.predicate = predicate
    #     if (object is not None):
    #         self.obj = literal(object.toString(), "")
    #     else:
    #         self.obj = None
        
    

    '''
     * Return the subject constraint of self selector.
     *
     * @return the subject constraint
    '''
    # @Override
    # param 
    # result Resource
    def getSubject(self):
        return self.subject
    

    '''
     * Return the predicate constraint of self selector.
     *
     * @return the predicate constraint
    '''
    # @Override
    # param 
    # result Property
    def getPredicate(self):
        return self.predicate
    

    '''
     * Return the object constraint of self selector.
     *
     * @return the object constraint
    '''
    # @Override
    # param 
    # result RDFNode
    def getObject(self):
        return self.obj
    

    '''
     * Answer True iff self Selector is completely characterised by its
     * S/P/O triple. Subclasses will by default return False, so self method need not
     * be over-ridden (the only reason for subclassing SimpleSelector is to make
     * a test not dependent only on the S/P/O identity).
     *
     * @return True iff self selector only depends on S/P/O identity.
    '''
    # @Override
    # param 
    # result boolean
    def isSimple(self):
        return self.__class__ == SimpleSelector.__class__
    

    '''
     * Test whether a statement should be included in a selection.  This method
     * tests whether the supplied statement satisfies the subject, predicate and
     * object constraints of the selector and then tests whether it matches the
     * application provided <CODE>selects</CODE> method.
     *
     * @param s the statement to be tested
     * @return True if the statement satisfies the subject, object
     * and predicate constraints and the selects constraint.
    '''
    # @Override
    # param Statement s
    # result boolean
    def test(self, s):
        return (self.subject is None or self.subject.equals(s.getSubject())) \
                and (self.predicate is None or self.predicate.equals(s.getPredicate())) \
                and (self.obj is None or self.obj.equals(s.getObject())) \
                and self.selects(s)
    

    '''
     * This method is designed to be over ridden by subclasses to define application
     * specific constraints on the statements selected.
     *
     * @param s the statement to be tested
     * @return True if the statement satisfies the constraint
    '''
    # param Statement s
    # result boolean
    def selects(self, s):
        return True
    


